#!/usr/bin/bash

# ************************************************
# @author: LANSHANQUAN
# @date: 2023-12-06
# @modify: 2023-12-18
# @function: operator app more easily!
# ************************************************

function start() {
    pid=`ps -ef | grep ConcurrentService | grep -v grep | awk '{printf "%s",$2}'`

    if [[ -n ${pid} ]]
    then
        echo "[WARN] App has already started!"
	exit 1
    fi

    nohup java -jar -Xms1024m -Xmx1024m -XX:MaxMetaspaceSize=256m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC ConcurrentService-0.0.1-SNAPSHOT.jar &

    pid=`ps -ef | grep ConcurrentService | grep -v grep | awk '{printf "%s",$2}'`

    if [[ -z ${pid} ]]
    then
        echo "[WARN] Start app failed!"
    else
        echo "[WARN] Start app success!"
    fi
}

function stop() {
    pid=`ps -ef | grep ConcurrentService | grep -v grep | awk '{printf "%s",$2}'`

    if [[ -n ${pid} ]]
    then
        echo "[WARN] Stop app..."
        kill -9 ${pid}
    fi

    status
}

function restart() {
    stop

    start

    status
}

function status() {
    pid=`ps -ef | grep ConcurrentService | grep -v grep | awk '{printf "%s",$2}'`

    if [[ -z ${pid} ]]
    then
        echo "[WARN] App is stopped!"
    else
        echo "[INFO] App is running..."
    fi 
}

function usage() {
    echo "[Usage] bash MyAppUtil.sh start"
}

function main() {
    if [[ $1 != "start" && $1 != "stop" && $1 != "restart" && $1 != "status" ]]
    then
        echo "[ERROR] parameter error!"
	usage
	exit 1
    fi

    if [[ $1 == "start" ]]
    then
        start
    elif [[ $1 == "stop" ]]
    then
        stop
    elif [[ $1 == "restart" ]]
    then
        restart
    else
        status
    fi
}

main $@
